<?php
/**
 * 文章控制器
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/7/15
 * Time: 下午5:22
 */

namespace Superadmin\Controller;


use Org\Util\Tree;

class ArticleController extends BaseController
{
    private $mod;
    public function __construct()
    {
        parent::__construct();
        $this->mod = D('article');
    }

    public function index()
    {
        $where = 1;
        $where = $this->makewhere($where);
        $order = '';
        $order = $this->makeorder($order);
        $list = $this->getpage($this->mod, $where, $order, C('page_size'));
        $this->assign('list', $list['list']);
        $this->assign('page', $list['page']);
        $big_menu = array(
            'title' => L('添加文章'),
            'uri' => U('article/add'),
        );
        $this->assign('big_menu', $big_menu);
        //可用分类信息
        $cate_list = S('cache_cate_list');
        if(!$cate_list)
        {
            $cate_list = D('article_cate')->getallcate();
        }
        $cate_path = D('article_cate')->where(array('id' => I('cate_id')))->getField('cate_path');
        $this->assign('cate_path', $cate_path);
        $this->assign('cate_list', changeArrIndex(D('article_cate')->getallcate(false), 'id'));
        $this->assign('json_cate_list', json_encode($cate_list));
        $this->display();
    }

    //构造查询条件
    private function makeWhere($where)
    {
        $time_start = I('time_start');
        $time_end   = I('time_end');
        $cate_id    = I('cate_id');
        $status     = I('status');
        $keyword    = I('keyword');
        if($time_start) $time_start = strtotime($time_start);
        if($time_end)   $time_end = strtotime($time_end);
        if($time_start && !$time_end)
        {
            $where .= ' and addtime between '.$time_start.' and '.($time_start+86400);
        }
        if($time_end && !$time_start)
        {
            $where .= ' and addtime between '.$time_end.' and '.($time_end+86400);
        }
        if($time_start && $time_end)
        {
            if($time_start < $time_end) list($time_start, $time_end) = array($time_end, $time_start );
            $where .= ' and addtime between '.$time_start.' and '.($time_end+86400);
        }

        if(isset($_GET['status']) && $status != 'null')
        {
            $where .= ' and status = '. $status;
        }
        if($keyword)
        {
            $where .= ' and title like "%'.$keyword.'%"';
        }
        if($cate_id != 'null' && $cate_id != '')
        {
            //查询所有子集
            $child_ids = D('article_cate')->where('cate_path like"%'.$cate_id.'|%"')->field('id')->select();
            if($child_ids)
            {
                $child_ids = i_array_column($child_ids, 'id');
            }
            array_push($child_ids, $cate_id);
            $where .= ' and cate_id in('.implode(',',$child_ids).')';
            $this->assign('cate_id', $cate_id);
        }
        return $where;
    }

    //构造排序条件
    private function makeOrder($order)
    {
        $sort = I('sort');
        $by   = I('order');
        if($sort && $by)$order = $sort .' '. $by;
        return $order;
    }


    public function add()
    {
        if(IS_POST)
        {
            if(!$this->mod->create())
            {
                $msg = $this->mod->getError();
                $this->error($msg, U('superadmin/article/add'));
            } else {
                $this->mod->addtime = time();
                $this->mod->thumb   = I('img');
                $id =  $this->mod->add();
                $article_content = D('article_content');
                if(!$article_content->create())
                {
                    $msg = $article_content->getError();
                    $this->error($msg, U('superadmin/article/add'));
                } else {
                    $article_content->article_id = $id;
                    $article_content->add();
                }
            }
           $this->success(L('operation_success'), U('superadmin/article/add'));
        }
        //分类信息
        $cate_list = S('cache_cate_list');
        if(!$cate_list)
        {
            $cate_list = D('article_cate')->getallcate();
        }
        $this->assign('cate_list', json_encode($cate_list));
        $this->display();
    }


    public function edit()
    {
        $article_id = I('id');
        $article = $this->mod->find($article_id);
        if(!$article)
        {
            $this->error(L('no_source'), U('superadmin/article/index'));
        }
        if(IS_POST)
        {
            if(!$this->mod->create())
            {
                $msg = $this->mod->getError();
                $this->error($msg, U('superadmin/article/add'));
            } else {
                $this->mod->addtime = time();
                $this->mod->thumb   = I('img');
                $this->mod->where(array('id' => $article_id))->save();
                $article_content = D('article_content');
                if(!$article_content->create())
                {
                    $msg = $article_content->getError();
                    $this->error($msg, U('superadmin/article/add'));
                } else {
                    $article_content->article_id = $article_id;
                    $article_content->where(array('article_id' => $article_id))->save();
                }
            }
            $this->success(L('operation_success'), U('superadmin/article/edit',array('id' => $article_id)));

        }


        $article['content'] = D('article_content')->where(array('article_id' => $article['id']))->getField('content');
        //分类信息
        $cate_list = S('cache_cate_list');
        S('cache_cate_list',null);
        if(!$cate_list)
        {
            $cate_list = D('article_cate')->getallcate();
        }
        $article['cate_path'] = D('article_cate')->where(array('id' => $article['cate_id']))->getField('cate_path');
        $this->assign('article', $article);
        $this->assign('cate_list', json_encode($cate_list));
        $this->display();
    }

    //todo 回收站
    public function recovery()
    {

    }

    //单页管理
    public function page()
    {
        $sort = 'ordid asc';
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $cate_ids   = D('article_cate')->where(array('status' => 1, 'type' => 2))->select();
        $list = array();
        if($cate_ids)
        {
            $cate_ids   = i_array_column($cate_ids, 'id');
            $result = D('article_cate')->where(array('id' => array('in', $cate_ids)))->order($sort)->select();

            $array = array();
            foreach($result as $r) {
                //是否有下一级
                if (D('article_cate')->where(array('pid'=>$r['id']))->count('id')) {
                    $r['str_manage'] = '';
                } else {
                    $r['str_manage'] = '<a href="'.U('article/page_edit', array('cate_id'=>$r['id'])).'">'.L('edit').'</a>';
                }
                $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
                $array[] = $r;
            }
            $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'>\$id</td>
                <td>\$spacer\$cate_name</td>
                <td align='center'>\$str_manage</td>
                </tr>";
            $tree->init($array);
            $list = $tree->get_tree(0, $str);

        }

        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }

    /**
     * 单页编辑
     */
    public function page_edit()
    {
        $cate_id = I('cate_id');
        if(!$cate_id)
        {
            $this->error(L('illegal_operation'), U('superadmin/article/page'));
        }
        $info = D('single_page')->where(array('cate_id' => $cate_id))->find();
        if(IS_POST)
        {
            $data = array(
                'title'   => I('title'),
                'content' => I('content'),
                'seo_title'       => I('seo_title'),
                'seo_keywords'    => I('seo_keywords'),
                'seo_description' => I('seo_description'),
                'addtime' => time(),
            );
            if($info)
            {
                // save
                D('single_page')->where(array('cate_id' => $cate_id))->save($data);
            } else {
                // add
                $data['cate_id'] = $cate_id;
                D('single_page')->add($data);

            }
            $this->success(L('operation_success'), U('superadmin/article/page'));
        }
        $this->assign('info',$info);
        $this->display();
    }

    //删除－物理删除
    public function delete()
    {
        $id = I('id');
        D('article')->where(array('id' => $id))->delete();
        D('article_content')->where(array('article_id' => $id))->delete();
        $return = array(
            'status' => 1,
            'msg'    => L('operation_success')
        );
        $this->ajaxReturn($return);
    }
}